package com.songqintuan.platform.genl;

import java.io.Serializable;
import java.util.Iterator;
import java.util.List;

import javax.faces.bean.ApplicationScoped;
import javax.faces.bean.ManagedBean;
import javax.faces.model.SelectItem;

import com.songqintuan.common.util.SpringFacesUtil;
import com.songqintuan.model.po.Organization;
import com.songqintuan.persist.service.hibernate.IOrganizationService;
import com.songqintuan.platform.common.constant.PlatformSpringBeanConstants;

@ManagedBean(name="existOrganizationListBox")
@ApplicationScoped
public class ExistOrganizationListBoxBean implements Serializable {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private List<Organization> listDatas;
	
	private SelectItem[] items;



	public List<Organization> getListDatas() {
		return listDatas;
	}



	public void setListDatas(List<Organization> listDatas) {
		this.listDatas = listDatas;
	}



	public SelectItem[] getItems() {
		return items;
	}



	public void setItems(SelectItem[] items) {
		this.items = items;
	}



	public ExistOrganizationListBoxBean() {
		IOrganizationService<Organization,Long> objService = (IOrganizationService<Organization,Long>)SpringFacesUtil.getSpringBean(PlatformSpringBeanConstants.ORGANIZATION_SERVICE_BEAN_NAME);
		objService.setEntityClass(Organization.class);
		objService.setPKClass(Long.class);
		String hql = "from Organization org order by org.struct asc,org.sort asc";
		List<Organization> results = objService.find(hql, null);
		this.setListDatas(results);
		SelectItem[] tempItems = null;
		if (null != results && !(results.isEmpty())) {
			tempItems = new SelectItem[results.size()];
			for(int i=0; i < results.size();i++) {
				Organization obj = results.get(i);
				tempItems[i] = new SelectItem(obj.getId(),addChars(obj.getLevel(),obj.getLabel()));
			}
		}
		this.setItems(tempItems);
	}

	private String addChars(Integer level,String label){
		int length = level.intValue();
		StringBuffer resultBuff = new StringBuffer();
		if (length > 0) {
			for (int j=0;j<length;j++) {
				resultBuff.append("---");
			}
		}
		resultBuff.append(label);
		return resultBuff.toString();
	}
	
}
